import router from "@/router/index.ts";
import { createVNode, ref, render, nextTick } from "vue";
import lang from "@/lang/index.ts";
const { t } = lang.global;

export let openPage = (path: string, query = {}) => {
  router.push({ path, query });
};
export let back = () => {
  router.go(-1);
};
export let cteatedmodel = () => {
  const container = document.createElement("div");
  const vm = createVNode("zloading", {});
  render(vm, container);
  document.body.appendChild(container);
};
// 获取assets静态资源
export const getAssetsFile = (url: string) => {
  console.log(
    new URL(`../assets/img/${url}`, import.meta.url).href,
    import.meta.url
  );

  return new URL(`../assets/img/${url}`, import.meta.url).href;
};
export let getTime = (val: string) => {
  const currentTime = new Date();
  const year = currentTime.getFullYear();
  const month = currentTime.getMonth() + 1; // 月份从0开始，因此需要加1
  const day = currentTime.getDate();
  const hours = currentTime.getHours();
  const minutes = currentTime.getMinutes();
  const seconds = currentTime.getSeconds();
  return (
    year + val + month + val + day 
  );
  // return (
  //   year + val + month + val + day + " " + hours + ":" + minutes + ":" + seconds
  // );
};
export async function copyToClipboard(text: string | number, proxy) {
  let value = text.toString();
  if (value) {
    await proxy
      .$copyText(value)
      .then(() => {
        ElMessage({
          message: t("language.commontips.copysuccess"),
          type: "success",
          duration: 1000,
        });
      })
      .catch(() => {
        ElMessage({
          message: t("language.commontips.copyfail"),
          type: "error",
          duration: 1000,
        });
      });
  } else {
    ElMessage({
      message: t("language.commontips.copysempty"),
      type: "error",
    });
  }
}
//获取ip
export let getipconfig = async () => {
  let ip = ref(null);
  await new Promise((resolve) => {
    nextTick(() => {
      resolve(window.location.host);
      // console.log(ip.value, "ip.value");
    });
  }).then((res) => {
    ip.value = res;
  });
  console.log(ip.value, "ip.value32233");

  return ip.value;
};
export let dot = (data) => {
  let str:string="0.00"
  if(data==0||data==''||data==undefined||data==null) return str
  str = (Number(data) / 100).toFixed(2).toString();
  return str.includes(".") ? str : str + ".00";
};
